package com.distance;

import com.claim.SubmitClaimClient;
import com.settings.LoginClient;
import com.settings.SettingsAdapter;
import com.settings.SettingsParameters;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Settings extends Activity {
    private EditText settingsUsernameEditText;
    private EditText settingsPasswordEditText;
    private Button settingsButton;
    private LoginClient login;
    private ProgressDialog loginProgressDialog;
    private Thread submitLoginThread;
    private Thread timerThread;
    private String loginString;
    private SubmitLoginHandler submitLoginHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.settings);
	settingsUsernameEditText = (EditText)findViewById(R.id.SettingsETUserName);
	settingsPasswordEditText = (EditText)findViewById(R.id.SettingsETPassword);
	settingsButton = (Button)findViewById(R.id.SettingsButton);
	if(SettingsParameters.loginValidated) {
	    settingsUsernameEditText.setText(SettingsParameters.username);
	    settingsPasswordEditText.setText(SettingsParameters.password);
	}
	submitLoginHandler = new SubmitLoginHandler();
	this.settingsButton.setOnClickListener(
		new OnClickListener(){
		    public void onClick(View v) {
			try{
			    login = new LoginClient();
			    loginProgressDialog = ProgressDialog.show(Settings.this, "Please Wait", "Verifying Login", true, false);
			    submitLoginThread = new Thread() {
				public void run(){
				    //String addressInput = address.getText().toString(); // Get input text
				    try {
					loginString = login.login(settingsUsernameEditText.getText().toString(), settingsPasswordEditText.getText().toString());
					//submitLoginHandler.sendEmptyMessage(0);
					//loginProgressDialog.dismiss();
				    } catch (Exception e) {
				    }
				}
			    };

			    timerThread = new Thread() {
				public void run(){
				    try {
					Thread.sleep(5000);
					submitLoginThread = null;
					submitLoginHandler.sendEmptyMessage(0);
					//loginProgressDialog.dismiss();
					//settingsButton.setText(loginString);
				    } catch (Exception e) {
				    }
				}
			    };
			    submitLoginThread.start();
			    timerThread.start();

			    //settingsUsernameEditText.

			}catch(Exception e){

			}
		    }
		}
	);
    }

    private class SubmitLoginHandler extends Handler{

	@Override
	public void handleMessage(Message msg) {
	    loginProgressDialog.dismiss();
	    if(loginString==null){
		new AlertDialog.Builder(Settings.this)
		.setTitle("Login")
		.setMessage("Unable to connect to server.\nPlease try again.")
		.setPositiveButton("OK",null)
		.create()
		.show();
	    }else if(loginString.equals("success")){
		SettingsParameters.password = settingsPasswordEditText.getText().toString();
		SettingsParameters.username = settingsUsernameEditText.getText().toString();
		SettingsParameters.loginValidated = true;
		SettingsAdapter.saveSettings();
		new AlertDialog.Builder(Settings.this)
		.setTitle("Login")
		.setMessage("Login Successful. Your login has been saved.")
		.setPositiveButton("OK",null)
		.create()
		.show();
	    }else if(loginString.equals("fail")){
		SettingsParameters.loginValidated = false;
		SettingsParameters.password = "";
		SettingsParameters.username = "";
		SettingsAdapter.saveSettings();
		new AlertDialog.Builder(Settings.this)
		.setTitle("Login Error")
		.setMessage("Your username/password combination is wrong.")
		.setPositiveButton("OK",null)
		.create()
		.show();
	    }
	    loginString = null;
	    super.handleMessage(msg);
	}

    }

}
